<?php
/**
 * Created by PhpStorm.
 * User: readLine
 * Date: 2017/11/7
 * Time: 9:39
 */

namespace app\index\controller;

use think\Controller;
use think\Db;
use \think\Session;

//著作信息的操作
class Monograph extends Controller
{
    //显示图书管理界面
    public function index(){
        $level=Session::get('user');
        $this->assign('level',$level->level);
        return view('manageMonograph');
    }
    //图书信息修改
    public function changeMonograph(){
        $id = input('id');
        $this->assign('id',$id);

        $ret = Db::table('monograph')->where('id',$id)->find();
        $this->assign('book_name',$ret['book_name']);
        $this->assign('book_author',$ret['book_author']);
        $this->assign('book_company',$ret['book_company']);
        $this->assign('author_about',$ret['author_about']);
        $this->assign('book_date',$ret['book_date']);
        $this->assign('book_about',$ret['book_about']);
        $this->assign('status',$ret['status']);
        $this->assign('book_word_count',$ret['book_word_count']);

        return view();
    }

    //获取著作信息
    public function getMonographInfo(){

        $ret['code'] = 1;
        $ret['msg'] = '';

        $page = input('page');
        $limit = input('limit');

        $start = ($page-1)*$limit;

        $ret['data'] = Db::table('monograph')
            ->limit($start,$limit)
            ->field('Id,book_name,book_author,status')
            ->select();
        $ret['count'] = count(Db::table('monograph')->select());
        if($ret){
            $ret['msg'] = '获取著作信息成功';
            return json($ret);
        }
        else{
            $ret['code'] = 0;
            $ret['msg'] = '获取著作信息失败';
            return json($ret);
        }

    }
    //获取该账号录入的著作信息
    public function getMonographInfoMe(){

        $ret['code'] = 1;
        $ret['msg'] = '';
        $user=Session::get('user');
        $page = input('page');
        $limit = input('limit');
        $start = ($page-1)*$limit;
        $ret['data'] = Db::table('monograph')
            ->where('user','=',$user->name)
            ->limit($start,$limit)
            ->field('Id,book_name,book_author,status')
            ->select();
        $ret['count'] = count(Db::table('monograph')->select());
        if($ret){
            $ret['msg'] = '获取著作信息成功';
            return json($ret);
        }
        else{
            $ret['code'] = 0;
            $ret['msg'] = '获取著作信息失败';
            return json($ret);
        }

    }














    //获取一本著作的全部信息
    public function getMonograph(){

        $id = input('id');

        $ret = Db::table('monograph')->where($id)->find();

        if($ret){
            $this->success('查找成功','',$ret);
        }
        else{
            $this->error('查找失败');
        }
        
    }

    //插入著作
    public function addMonograph(){

        $insert['book_name'] = input('book_name');
        $insert['book_company'] = input('book_company');
        $insert['book_author'] = input('book_author');
        $insert['author_about'] = input('author_about');
        $insert['book_date'] = input('book_date');
        $insert['book_about'] = input('book_about');
        $insert['book_word_count'] = input('book_word_count');
        $insert['status'] = input('status');
        $insert['user'] = input('user');

        $isInsert = Db::table('monograph')->where('book_name',input('book_name'))->select();

        if($isInsert){
            $this->error('重复插入');
        }
        $data = Db::table('monograph')->insert($insert);

        if($data){
            $ret['code']='1';
            $ret['msg']='插入成功';
            return json($ret);
//            $this->success('插入成功');
        }
        else{
            $this->error('插入失败');
        }

    }

    //编辑著作
    public function editMonograph(){
        $id = input('id');
        $data['book_name'] = input('book_name');
        $data['book_company'] = input('book_company');
        $data['book_author'] = input('book_author');
        $data['author_about'] = input('author_about');
        $data['book_date'] = input('book_date');
        $data['book_about'] = input('book_about');
        $data['book_word_count'] = input('book_word_count');
        $data['status'] = input('status');

        $ret = Db::table('monograph')->where('id',$id)->update($data);

        if($ret){
            $this->success('更改成功');
        }
        else{
            $this->error('更改失败');
        }

    }

    //删除著作
    public function delMonograph(){

        $id = input('id');

        $ret = Db::table('monograph')->delete($id);

        if($ret){
            $this->success('删除成功');
        }
        else{
            $this->error('删除失败');
        }

    }

}